import java.util.Arrays;

/**
 * Description:
 * Date: 2025-11-02
 * Time: 16:24
 */
public class MyArrayList implements IList{
    //底层存放数据的原理就是数组
   private int[] array;
   //记录当前数组中有多少个元素
   public int usedSize;

   public static final int DEFAULT_CAPACITY = 10;
    public MyArrayList() {
        //当new一个新对象时，就给MyArrAyList开辟一块储存空间
        //若需要修改默认容量，直接修改DEFAULT_CAPACITY即可，否则可能需要修改全部int[]中的数字
        this.array = new int[DEFAULT_CAPACITY];
    }


    @Override
    public void add(int data) {
        //默认就是尾插法

        //判满
        if (isFull()){
            //扩容
            copyArray();
        }

        this.array[usedSize] = data;
        usedSize++;
    }

    private boolean isFull() {
        return this.array.length == usedSize;
    }

    private void copyArray() {
        this.array = Arrays.copyOf(this.array, 2 * DEFAULT_CAPACITY);
    }

    @Override
    public void add(int pos, int data) {
        int len = size();
        //java底层机制规定，每次插入数据，前驱必须是存在的
        if (pos <=0 || pos > len+1){
            System.out.println("插入的位置错误！");
            return;
        }

        if(isFull()){
            copyArray();
        }

        if(pos == len+1){
            add(data);
            return;
        }

        for (int i = len-1; i >= pos-1; i--) {
            this.array[i+1] = this.array[i];
        }
        this.array[pos-1] = data;
        this.usedSize++;
    }

    private boolean isEmpty() {
        return this.usedSize == 0;
    }

    @Override
    public boolean contains(int toFind) {
        if (isEmpty()){
            System.out.println("表为空！");
            return false;
        }
        int len = size();
        for (int i = 0; i < len; i++) {
            int tar = array[i];
            if(toFind == tar){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        if (isEmpty()){
            System.out.println("表为空！");
            return -1;
        }
        int len = size();
        for (int i = 0; i < len; i++) {
            int tar = array[i];
            if(toFind == tar){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        if (isEmpty()){
            System.out.println("表为空！");
            return -1;
        }
        int len = size();
        if (pos <= 0 || pos > len){
            System.out.println("pos位置出错！");
            return -1;
        }

        int ret = this.array[pos-1];
        return 0;
    }

    @Override
    public void set(int pos, int value) {
        if (isEmpty()){
            System.out.println("表为空！");
            return;
        }
        int len = size();
        if (pos <= 0 || pos > len) {
            System.out.println("放置的位置出错！");
            return;
        }


        this.array[pos-1] = value;
    }

    @Override
    public void remove(int toRemove) {
        //元素集体向前挪一位
        if (isEmpty()){
            System.out.println("表为空！");
            return;
        }

        int len = size();
        int cur = 0;
        boolean fal = true;
        for (int i = 0; i < len; i++) {
            int tar = array[i];
            if (tar == toRemove) {
                cur = i;
                fal = false;
            }
        }

        if (fal) {
            System.out.println("没有你要删的元素！");
            return;
        }

        if (cur == this.usedSize){
            usedSize--;
            return;
        }

        for (int i = cur; i < len - 1; i++) {
            array[i] = array[i+1];
        }
        this.usedSize--;

    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {
        /*for (int i = 0; i < size(); i++) {
            array[i] == null;
        }*/

        this.usedSize = 0;
    }

    @Override
    public void display() {
        int len = size();
        for (int i = 0; i < len; i++) {
            System.out.print(this.array[i] + " ");
        }
        System.out.println();
    }
}
